"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transaction = exports.query = void 0;
const mysql_1 = __importDefault(require("mysql"));
const fs_1 = __importDefault(require("fs"));
const utils_1 = require("./utils");
/**
 * 建立连接池，封装connection
 * 封装两种类型的sql房屋方法，普通语句访问以及事物访问
 */
let pool;
const configPath = "sqlconfig.json";
const config = JSON.parse(fs_1.default.readFileSync(configPath).toString());
pool = mysql_1.default.createPool({
    host: config.host,
    port: config.port,
    user: config.user,
    password: config.password,
    database: config.database,
    multipleStatements: true
});
async function getSqlConnection() {
    let connection;
    return new Promise(resolve => {
        pool.getConnection((err, conn) => {
            connection = conn;
            resolve(connection);
        });
    });
}
/**
 * sql语句方式访问数据库
 * @param sqlstr 访问语句
 * @param sqlParams params参数
 */
async function query(sqlstr, sqlParams) {
    const connection = await getSqlConnection();
    const done = new Promise((resolve, reject) => {
        const conn = connection.query(sqlstr, sqlParams, function (error, results) {
            if (error) {
                reject(error);
            }
            resolve(results);
        });
        //console.log(conn.sql);
    });
    connection.release();
    return done;
}
exports.query = query;
/**
 * 执行事物操作基本操作
 */
function doTran(sqlstrs, sqlParams, conn, n, max, resolve, reject, paramsData) {
    let ps = utils_1.transParams(sqlParams[n], paramsData[n]);
    const c = conn.query(sqlstrs[n], ps, (err, res) => {
        if (err) {
            reject(err);
            conn.rollback((err) => {
                if (err) {
                    console.log(err);
                }
            });
        }
        if (n < max) {
            doTran(sqlstrs, sqlParams, conn, n + 1, max, resolve, reject, paramsData);
        }
        else {
            resolve(res);
            conn.commit((err) => {
                if (err) {
                    console.log(err);
                }
            });
        }
    });
    //console.log(c.sql);
}
/**
 * 事物操作方法
 * @param sqlstrs 参与事物的sql语句
 * @param sqlParams 对应参数字段
 * @param paramsData 对应参数数据
 */
async function transaction(sqlstrs, sqlParams, paramsData) {
    const connection = await getSqlConnection();
    const done = new Promise((resolve, reject) => {
        if (sqlstrs.length == sqlParams.length) {
            connection.beginTransaction((err) => {
                if (err) {
                    console.log(err);
                }
                doTran(sqlstrs, sqlParams, connection, 0, sqlstrs.length - 1, resolve, reject, paramsData);
            });
        }
        else {
            reject(new Error("sql与params的length不匹配!"));
        }
    });
    connection.release();
    return done;
}
exports.transaction = transaction;
